package com.personal.system.service.impl.content;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.personal.common.result.PageUtils;
import com.personal.common.result.Result;
import com.personal.system.entity.content.Category;
import com.personal.system.entity.vo.SystemCategoryListVO;
import com.personal.system.exception.GlobalException;
import com.personal.system.mapper.content.CategoryMapper;
import com.personal.system.service.content.CategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 分类列表
     * @param name 分类名
     * @return
     */
    @Override
    public Result selectCategoryList(String name) {
        Page<SystemCategoryListVO> categoryPage = categoryMapper.selectCategory(new Page<>(PageUtils.getPageNo(), PageUtils.getPageSize()), name);
        return Result.ok(categoryPage);
    }

    /**
     * 分类详情
     * @param id 分类id
     * @return
     */
    @Override
    public Result getCategoryById(Long id) {
        Category category = categoryMapper.selectById(id);
        return Result.ok(category);
    }
    /**
     * 添加分类
     * @param category 分类对象
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result insertCategory(Category category) {
        Category vo = categoryMapper.selectOne(new QueryWrapper<Category>().eq("name", category.getName()));
        if (vo != null) {
            throw new GlobalException(404,"该分类名称已存在!");
        }
        int rows = categoryMapper.insert(category);
        return rows > 0 ? Result.ok(): Result.fail("添加分类失败");
    }

    /**
     * 修改分类
     * @param category 分类对象
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateCategory(Category category) {
        Category vo = categoryMapper.selectOne(new QueryWrapper<Category>().eq("name", category.getName()));
        if (vo!= null && !vo.getId().equals(category.getId())) {
            throw new GlobalException(404,"该分类名称已存在!");
        }

        int rows = categoryMapper.updateById(category);
        return rows > 0 ? Result.ok(): Result.fail("修改分类失败");
    }

    /**
     * 删除分类
     * @param id 分类id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteCategory(Long id) {
        int rows = categoryMapper.deleteById(id);
        return rows > 0 ? Result.ok(): Result.fail("删除分类失败");
    }

    /**
     * 批量删除分类
     * @param list
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteBatch(List<Category> list) {
        List<Long> ids = list.stream().map(Category::getId).collect(Collectors.toList());
        int rows = categoryMapper.deleteBatchIds(ids);
        return rows > 0 ? Result.ok(): Result.fail("批量删除分类失败");
    }

    /**
     * 置顶分类
     * @return 置顶分类
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result top(Long id) {
        Category category = categoryMapper.selectOne(new QueryWrapper<Category>().orderByDesc("sort").last("limit 1"));
        if (category.getId().equals(id)) {
            throw new GlobalException(404,"该分类已经在顶端!!");
        }

        Category vo = Category.builder()
                .sort(category.getSort() + 1).updateTime(DateUtil.date()).id(id).build();
        int rows = categoryMapper.updateById(vo);

        return rows > 0 ? Result.ok(): Result.fail("置顶失败");
    }
}
